﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using MMM.Core.Domain;
using ServiceStack.ServiceHost;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MMM.Services.Base;
using MMM.Services.Operations;
using MMM.Services.Type;
using MMM.Core;
using MMM.Services.Response;
using ServiceStack.OrmLite;
using ServiceStack.ServiceInterface;


namespace MMM.Services
{
    public class UserRoleService : RestServiceBase<UserRole>
    {
        public override object OnGet(UserRole request)
        {
            IRepository<Role> roleRep = ApplicationContext.Container.GetInstance<IRepository<Role>>();
            IRepository<RoleUser> roleuserRep = ApplicationContext.Container.GetInstance<IRepository<RoleUser>>();
            List<Guid?> dic = new List<Guid?>();
            IEnumerable<Role> role = roleRep.GetAll();
            if (!string.IsNullOrEmpty( request.User))
            {
                Guid g = Guid.Parse(request.User);
                IEnumerable<RoleUser> roleUser = roleuserRep.GetAll(t => t.UserID ==g);
                foreach (var item in roleUser)
                {
                    dic.Add(item.RoleID);
                }
                List<UserRole> lst = new List<UserRole>();
                UserRole user = null;
                if (role.Count() > 0)
                {
                    foreach (var item in role)
                    {
                        user = new UserRole();
                        if (dic.Contains(item.Id))
                        {
                            user.IsChecked = true;
                        }
                        user.Id = item.Id;
                        user.Flag = item.Flag;
                        user.Index = item.Index;
                        user.Name = item.Name;
                        lst.Add(user);
                    }
                }
                return lst;
            }
            else
            {
                List<UserRole> lst = new List<UserRole>();
                UserRole user = null;
                if (role.Count() > 0)
                {
                    foreach (var item in role)
                    {
                        user = new UserRole();
                        user.IsChecked = false;
                        user.Id = item.Id;
                        user.Flag = item.Flag;
                        user.Index = item.Index;
                        user.Name = item.Name;
                        user.User = request.User;
                        
                        lst.Add(user);
                    }
                }
                return lst;
            }
            //UserRole user = new UserRole();
        }
    }

    public class UserRoleListService : RestServiceBase<UserRoleList> {
        public override object OnPut(UserRoleList request)
        {
            IRepository<RoleUser> roleRep = ApplicationContext.Container.GetInstance<IRepository<RoleUser>>();
            foreach (var item in request)
            {
                if (request != null)
                {
                    
                    string[] strs = request.User.Split('/');
                    Guid g = Guid.Parse(strs[0]);
                    IEnumerable<RoleUser> userPers = roleRep.GetAll(t => t.RoleID == item.Id.Value && t.UserID == g);
                    RoleUser roleUser = null;
                    if (userPers.Count() == 0)
                    {
                        roleUser = new RoleUser();
                        roleUser.Id = Guid.NewGuid();
                        roleUser.RoleID = item.Id;
                        roleUser.UserID = g;

                        roleRep.Add(roleUser);
                    }
                    else
                    {
                        Guid? DID = userPers.First().Id;
                        roleRep.DeleteByID(DID.Value);
                    }

                    //userPer.RoleID=
                }
            }
           
            return null;
        }
    }

    [Serializable]
    [Route("/api/userrole2", "GET")]
    public class UserRole : EntityBase<UserRole>
    {
        public UserRole():base()
        {
            IsChecked = false;
        }
        /// <summary>
        /// 名称
        /// </summary>
        [StringLength(50)]
        [Required]
        public String Name { get; set; }
        /// <summary>
        /// 显示顺序
        /// </summary>
        [Required]
        public int Index { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        [Required]
        public int Flag { get; set; }

        //public Guid? Id { get; set; }
        public string User { get; set; }
        public bool IsChecked { get; set; }
        //public Guid? RoleID { get; set; }
    }

    [Serializable]
    [Route("/api/userrole", "PUT")]
    public class UserRoleList:List<UserRole> {
        public string User { get; set; }
    }
}
